import { WebSocket } from "ws";
import { User } from "../../../types/User";
import { roomsTable } from "../../../database/rooms-table";
import { conversionMessage } from "../helpers/message-converter";
import { GetRoomStateMessageData } from "../types/MessageData";
import { deepClone } from "../../../utils/conversion";

export const getRoomStateHandler = ({ user, client }: { client: WebSocket; user: User; payload: GetRoomStateMessageData["payload"] }) => {
	const room = roomsTable.rooms.find((v) => v.players.some((v) => v.user.userId == user.userId));
	if (room) {
		const cloneRoom = deepClone(room);
		cloneRoom.players.forEach((v) => {
			v.client = null;
		});
		client.send(conversionMessage({ type: "gotRoomState", payload: cloneRoom }));
	}
};
